package com.gdust.lease.web.admin.controller.lease;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdust.lease.common.result.Result;
import com.gdust.lease.model.entity.ViewAppointment;
import com.gdust.lease.model.enums.AppointmentStatus;
import com.gdust.lease.web.admin.service.ViewAppointmentService;
import com.gdust.lease.web.admin.vo.appointment.AppointmentQueryVo;
import com.gdust.lease.web.admin.vo.appointment.AppointmentVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 预约看房管理接口
 */
@Tag(name = "预约看房管理")
@RequestMapping("/admin/appointment")
@RestController
public class ViewAppointmentController {
    //注入预约看房信息表的service
    @Autowired
    private ViewAppointmentService viewAppointmentService;


    /**
     * 分页查询预约信息
     *
     * 预约看房信息VO包含：预约公寓信息(公寓信息)
     *
     * @param current 当前页
     * @param size 每页记录数
     * @param queryVo 查询条件
     * @return
     */
    @Operation(summary = "分页查询预约信息")
    @GetMapping("page")
    public Result<IPage<AppointmentVo>> page(@RequestParam long current, @RequestParam long size, AppointmentQueryVo queryVo) {
        // 使用mybatis-plus分页插件创建分页对象，指定当前页和每页记录数
        IPage<AppointmentVo> page = new Page<>(current, size);
        //自定义方法：根据条件分页查询预约信息
        IPage<AppointmentVo> list = viewAppointmentService.pageAppointmentByQuery(page,queryVo);
        return Result.ok(list);
    }

    /**
     * 根据id更新预约状态
     * @param id 传入id
     * @param status 预约状态（1:待看房，2:已取消，3已看房）
     * @return
     */
    @Operation(summary = "根据id更新预约状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam AppointmentStatus status) {
        LambdaUpdateWrapper<ViewAppointment> viewAppointmentLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //匹配传入的id是否符合预约id
        viewAppointmentLambdaUpdateWrapper.eq(ViewAppointment::getId,id);
        //设置预约状态为传入的预约状态数值
        viewAppointmentLambdaUpdateWrapper.set(ViewAppointment::getAppointmentStatus,status);
        viewAppointmentService.update(viewAppointmentLambdaUpdateWrapper);
        return Result.ok();
    }

}
